# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>
#This value is here because some python package names have capital letters
#while there is a strong preference for lowercase letters in ArchLinux and
#MSYS packages.

_pyname=virtualenv
_realname=${_pyname} #This should be lowercase
pkgbase=mingw-w64-python2-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}")
pkgver=16.1.0
pkgrel=4
pkgdesc="Virtual Python Environment builder (mingw-w64)"
arch=('any')
url='https://virtualenv.pypa.io/'
license=('MIT')
install=${_realname}2-${CARCH}.install
depends=("${MINGW_PACKAGE_PREFIX}-python2")
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python2-pytest" "${MINGW_PACKAGE_PREFIX}-python2-mock")
options=('staticlibs' 'strip' '!debug')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/pypa/virtualenv/archive/${pkgver}.tar.gz
        001-mingw-python-support.patch)
sha256sums=('b906ef15b98227bfdacb73179932ee9640902f4721b53503e8e4cb1cf054cf4c'
            'ee800d34a0010f3cb143a1f87071400bc02111dfae5004aabeef771691b52a73')

prepare() {
  cd ${_pyname}-${pkgver}
  patch -p1 -i ${srcdir}/001-mingw-python-support.patch
  
  cd ${srcdir}
  rm -rf "python2-build-${CARCH}" | true
  cp -r "${_pyname}-${pkgver}" "python2-build-${CARCH}"
}
  
# Note that build() is sometimes skipped because it's done in 
# the packages setup.py install for simplicity if you can do so.
# but sometimes, you want to do a check before install which would
# also trigger the build.  
build() {
  msg "Python 2 build for ${CARCH}"
  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py build
}

#Uncomment when the prerequisites are finished.
#check() {  
#  msg "Python 2 test for ${CARCH}"
#  cd "${srcdir}/python2-build-${CARCH}"
#  PYTHONPATH="$PWD"  ${MINGW_PREFIX}/bin/py.test2
#}

package() {
  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/COPYING"

  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
  # fix python command in files
  for _ff in ${pkgdir}${MINGW_PREFIX}/bin/*.py; do
    sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i ${_ff}
  done
# This is for packages that install scripts and .exe into the /mingw*/bin
  for f in virtualenv; do
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}.exe
    if [ -f "${pkgdir}${MINGW_PREFIX}"/bin/${f}.exe.manifest ]; then
      mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}.exe.manifest
      sed -e "s|${f}|${f}2|g" -i "${pkgdir}${MINGW_PREFIX}"/bin/${f}2.exe.manifest
    fi
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}-script.py
  done
}
